// HTTP IPC 通信类型定义

// ==================== HTTP 基础类型 ====================

// HTTP 请求方法
export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH'

// HTTP 请求配置
export interface HttpRequestConfig {
  url: string
  method: HttpMethod
  headers?: Record<string, string>
  params?: Record<string, any> // URL 参数
  data?: any // 请求体数据
  timeout?: number
}

// HTTP 响应结构
export interface HttpResponse<T = any> {
  success: boolean
  data?: T
  error?: {
    code: string
    message: string
    details?: any
  }
  status?: number // HTTP 状态码
}

// ==================== 请求类型定义 ====================

// 认证相关请求
export interface LoginRequest {
  phone: string
  password: string
}

export interface RegisterRequest {
  phone: string
  password: string
  name?: string
}

export interface SendCodeRequest {
  phone: string
  type: number
}

export interface LoginWithCodeRequest {
  phone: string
  code: string
}

export interface RegisterWithCodeRequest {
  phone: string
  code: string
}

// 主页管理请求
export interface ProfileCreateRequest {
  url: string
}

export interface ProfileUpdateRequest {
  id: string
  updates: any
}

export interface ProfileDeleteRequest {
  id: string
}

export interface ProfileListRequest {
  // 暂时不需要参数
}

// 视频相关请求
export interface VideoListRequest {
  profileId?: string
}

// 抖音相关请求
export interface DouyinMainPageInfoRequest {
  url: string
}

// ==================== 响应类型定义 ====================

// 认证相关响应
export interface LoginResponse {
  user: any
  token: string
}

export interface RegisterResponse {
  user: any
  token: string
}

export interface SendCodeResponse {
  success: boolean
  message: string
}

export interface AuthResponse {
  user: any
  token: string
}

// 主页管理响应
export interface ProfileItem {
  id: string
  url: string
  name?: string
  platform?: string
  description?: string
  avatar?: string
  createdAt?: string
  updatedAt?: string
}

export interface ProfileListResponse {
  profiles: ProfileItem[]
}

export interface ProfileResponse {
  profile: ProfileItem
}

export interface ProfileDeleteResponse {
  // 删除操作通常返回空或成功状态
}

// 视频相关响应
export interface VideoItem {
  id: string
  title: string
  url: string
  cover?: string
  duration?: number
  createdAt?: string
  profileId?: string
}

export interface VideoListResponse {
  videos: VideoItem[]
}

// 抖音相关响应
export interface DouyinMainPageInfoResponse {
  author: string
  avatar: string
  description: string
  followerCount: number
  followingCount: number
  videoCount: number
}

// ==================== API 端点配置 ====================

export const API_ENDPOINTS = {
  // 认证相关
  AUTH_LOGIN: { method: 'POST' as const, path: '/api/auth/login' },
  AUTH_REGISTER: { method: 'POST' as const, path: '/api/auth/register' },
  AUTH_SEND_CODE: { method: 'POST' as const, path: '/api/auth/send-code' },
  AUTH_LOGIN_WITH_CODE: { method: 'POST' as const, path: '/api/auth/login-with-code' },
  AUTH_REGISTER_WITH_CODE: { method: 'POST' as const, path: '/api/auth/register-with-code' },

  // 主页管理
  PROFILE_GET_LIST: { method: 'GET' as const, path: '/api/homepage' },
  PROFILE_CREATE: { method: 'POST' as const, path: '/api/homepage' },
  PROFILE_UPDATE: { method: 'PUT' as const, path: '/api/homepage/:id' },
  PROFILE_DELETE: { method: 'DELETE' as const, path: '/api/homepage/:id' },

  // 视频相关
  VIDEO_GET_LIST: { method: 'GET' as const, path: '/api/videos' },

  // 抖音相关
  DOUYIN_MAIN_PAGE_INFO: { method: 'POST' as const, path: '/api/douyin/main-page-info' },
} as const

// ==================== 通用类型 ====================

// 通用 HTTP 请求函数类型
export type GenericHttpRequestFunction = <T = any>(
  config: HttpRequestConfig
) => Promise<HttpResponse<T>>

// API 路径常量（向后兼容）
export const API_PATHS = {
  // 认证
  LOGIN: '/api/auth/login',
  REGISTER: '/api/auth/register',
  SEND_CODE: '/api/auth/send-code',
  LOGIN_WITH_CODE: '/api/auth/login-with-code',
  REGISTER_WITH_CODE: '/api/auth/register-with-code',

  // 主页管理
  PROFILE_LIST: '/api/homepage',
  PROFILE_CREATE: '/api/homepage',
  PROFILE_UPDATE: '/api/homepage/:id',
  PROFILE_DELETE: '/api/homepage/:id',

  // 视频相关
  VIDEO_LIST: '/api/videos',

  // 抖音相关
  DOUYIN_MAIN_PAGE_INFO: '/api/douyin/main-page-info',
} as const

// ==================== 向后兼容的类型映射 ====================

// 为了保持向后兼容，提供类型映射
export type ApiRequestTypes = {
  login: LoginRequest
  register: RegisterRequest
  sendCode: SendCodeRequest
  loginWithCode: LoginWithCodeRequest
  registerWithCode: RegisterWithCodeRequest
  profileCreate: ProfileCreateRequest
  profileUpdate: ProfileUpdateRequest
  profileDelete: ProfileDeleteRequest
  profileList: ProfileListRequest
  videoList: VideoListRequest
  douyinMainPageInfo: DouyinMainPageInfoRequest
}

export type ApiResponseTypes = {
  login: LoginResponse
  register: RegisterResponse
  sendCode: SendCodeResponse
  loginWithCode: AuthResponse
  registerWithCode: AuthResponse
  profileList: ProfileListResponse
  profileCreate: ProfileResponse
  profileUpdate: ProfileResponse
  profileDelete: ProfileDeleteResponse
  videoList: VideoListResponse
  douyinMainPageInfo: DouyinMainPageInfoResponse
}